﻿namespace System;
internal class ParameterRebinder : ExpressionVisitor
{
    #region 私有属性
    private readonly Dictionary<ParameterExpression, ParameterExpression> map;
    #endregion
    #region Ctor
    internal ParameterRebinder(Dictionary<ParameterExpression, ParameterExpression> map)
    {
        this.map = map ?? new Dictionary<ParameterExpression, ParameterExpression>();
    }
    #endregion
    #region 类内访问静态方法
    internal static Expression ReplaceParameters(Dictionary<ParameterExpression, ParameterExpression> map, Expression exp) => new ParameterRebinder(map).Visit(exp);
    #endregion
    #region 受保护方法
    override protected Expression VisitParameter(ParameterExpression p)
    {
        if (map.TryGetValue(p, out var replacement))
        {
            p = replacement;
        }
        return base.VisitParameter(p);
    }
    #endregion
}
